home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cmln0985.arc
/
INTELHEX.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1986-02-27
|
6KB
|
166 lines
program intelhex;
{This program will take a file that is in the format produced by
the CP/M dump utility and produce a file in intel hex format
suitable for use with the CP/M load command.
To use: Type intelhex and respond to the prompts for file names.
Gary Sarff 10/03/84 BOX E Hood Center Norris City, Il 62869
}
type dumprec = record
addr : array[1..4] of char;
data : array[1..16] of
record
fill : char; {the blank between bytes}
item : array[1..2] of char
end;
end; {dumprec}
intelrec = record
linelength : array[1..2] of char;
addr : array[1..4] of char;
zeroes : array[1..2] of char;
data : array[1..16] of
array[1..2] of char;
checksum : array[1..2] of char;
end; {intelrec}
var
dumpline : dumprec;
intelline : intelrec;
dumpfile : text buffered(512); {512 byte buffer to speed up}
intelfile : text; {processing. can be expanded}
infile,
outfile : string; {used to get actual file names from user,
adjust the type here as needed.}
function hex_int(
var char_array : array[0..0] of char;
len_of_array : integer
):integer;
{This function returns the base 10
representation of the hex characters in
the array char_array. [0..0] means the
pascal compiler should accept any variable
of type array of char as an argument since we
are passing only a fixed length address here.
}
var i : integer;
value : integer;
begin {hex_int}
value:=0;
for i:=0 to len_of_array-1 do
case char_array[i] of
'0'..'9':value:=value*16+ord(char_array[i])-ord('0');
'A'..'F':value:=value*16+ord(char_array[i])-ord('A')+10
end; {case}
hex_int:=value
end; {hex_int}
procedure int_hex(
number : integer;
len_of_array : integer;
var char_array : array[0..0] of char
);
{This procedure will take the base 10 integer in
number and form a hex character representation of
it having length len_of_array and place the
characters into the array char_array.
}
var i : integer;
n : integer;
ch : char;
begin {int_hex}
for i:=len_of_array-1 downto 0 do
begin
n:=number mod 16;
case n of
0..9 : ch:=chr(ord('0')+n);
10..15 : ch:=chr(ord('A')+n-10);
end; {case}
char_array[i]:=ch;
number:=number div 16
end {for}
end; {int_hex}
procedure checksum(var rec : intelrec); {compute the 1 byte checksum for
rec and place it in rec.checksum}
var i : integer;
cks : integer; {holds the checksum during the loop}
len : integer; {length of rec}
begin {checksum}
cks:=hex_int(rec.linelength,2); {initialize checksum}
cks:=cks+hex_int(rec.addr,2)+hex_int(rec.addr,4)
-hex_int(rec.addr,2)*256;
len:=hex_int(rec.linelength,2);
for i:=1 to len do
cks:=cks+hex_int(rec.data[i],2);
cks:=255-(cks mod 256)+1; {We need two's comp. of checksum. That is
what CP/M load program expects to find.}
int_hex(cks,2,rec.checksum);
end; {checksum}
procedure init_files;
begin {init_files}
write("Enter the name of the input dump file -> ");
readln(infile);
reset(dumpfile,infile);
write("Enter the name of the output hex file -> ");
readln(outfile);
rewrite(intelfile,outfile);
end; {init_files}
procedure line_read; {Read a line from dumpfile into dumpline}
var i : integer;
begin
for i:=1 to 4 do {needed because this pascal won't
read array of char}
read(dumpfile,dumpline.addr[i]);
for i:=1 to 16 do
with dumpline.data[i] do
read(dumpfile,fill,item[1],item[2]);
readln(dumpfile);
end; {line_read}
procedure line_write; {Write the line in intelline to the file intelfile}
var i : integer;
begin
with intelline do
write(intelfile,':',linelength,addr,zeroes);
for i:=1 to 16 do
write(intelfile,intelline.data[i]);
write(intelfile,intelline.checksum);
write(intelfile,chr(13),chr(10)); {standard CP/M line terminators}
end; {line_write}
procedure move_line; {Move the data in dumpline into intelline}
var i : integer;
begin
with intelline do
begin
linelength:='10'; {always except on last line}
addr:=dumpline.addr;
zeroes:='00'; {unknown why these are needed}
for i:=1 to 16 do
data[i]:=dumpline.data[i].item;
end {with}
end; {move_line}
begin {main}
init_files;
while(not eof(dumpfile)) do
begin
line_read;
move_line;
checksum(intelline);
line_write
end; {while}
write(intelfile,':0000000000',chr(13),chr(10));
close(dumpfile);
close(intelfile)
end. {intelhex}